package com.weipai.education.controller;

import com.weipai.education.model.UserAddress;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/17 20:28
 * 
 * 前端个人信息控制类
 */
@RestController
@Validated
public class CenterController {
    
    @Autowired
    private UserService userService;

    /**
     * 获取用户的信息
     * @return
     */
    @RequestMapping(value = "/center/userInfo", method = RequestMethod.GET)
    public Response getUserInfo(){
        return userService.getUserInfo();
    }

    /**
     * 更新user的头像和昵称
     * @return
     */
    @RequestMapping(value = "/center/updateUser", method = RequestMethod.POST)
    public Response updateUser(@RequestParam(value = "nickname", required = false) String nickname,
                               @RequestParam(value = "headImg", required = false) String headImg){
        return userService.updateUser(nickname, headImg);
    }

    /**
     * 更改密码
     * @param oldPassword
     * @param newPassword
     * @param newTwoPassword
     * @return
     */
    @RequestMapping(value = "/center/updatePassword", method = RequestMethod.GET)
    public Response updatePassword(@NotBlank(message = "旧密码不能为空") String oldPassword,
                                   @NotBlank(message = "新密码不能为空") String newPassword,
                                   @NotBlank(message = "二次输入密码不能为空") String newTwoPassword){
        return userService.updatePassword(oldPassword, newPassword, newTwoPassword);
    }

    /**
     * 获取用户地址列表
     * @return
     */
    @RequestMapping(value = "/center/getUserAddress", method = RequestMethod.GET)
    public Response getUserAddress(@Valid String userId){
        return userService.getUserAddress(userId);
    }

    /**
     * 收货地址的新增--修改
     * @param userAddress
     * @return
     */
    @RequestMapping(value = "/center/userAddressSave", method = RequestMethod.POST)
    public Response userAddressSave(@Valid UserAddress userAddress){
        return userService.userAddressSave(userAddress);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @RequestMapping(value = "/center/deleteUserAddress", method = RequestMethod.GET)
    public Response deleteUserAddress(@NotBlank(message = "地址id不能为空") String id){
        return userService.deleteUserAddress(id);
    }
    
    
}
